package com.geekchique.runningbuddy.activity;

import java.util.List;
import java.util.LinkedList;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.service.RemoteStopClockServiceInterface;
import com.geekchique.runningbuddy.service.CannedRunManagerImpl;
import com.geekchique.runningbuddy.model.ListOfJogs;
import com.geekchique.runningbuddy.model.Jog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class DebugSettings extends Activity {
	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.debugsettings);

        //set up routes spinner
        ListOfJogs cannedJogs = CannedRunManagerImpl.readCannedJogs(this);

        List<String> routes = new LinkedList<String>();
        for (Jog j : cannedJogs)
        {
            routes.add(j.getName());
        }
        Spinner routeSpinner = (Spinner)findViewById(R.id.routeSpinner);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        routeSpinner.setAdapter(adapter);
        routeSpinner.setOnItemSelectedListener(routeSpinnerListener);
        routeSpinner.setSelection(RemoteStopClockServiceInterface.getInstance().getCannedRouteId());
        
        
        //set up speeds spinner
        List<String> speeds = new LinkedList<String>();
        speeds.add("crawl");
        speeds.add("walk");
        speeds.add("jog");
        speeds.add("run");
        speeds.add("sprint");
        
        Spinner speedSpinner = (Spinner)findViewById(R.id.speedSpinner);
        speedSpinner.setOnItemSelectedListener(speedSpinnerListener);

        ArrayAdapter<String> speedAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, speeds);
        speedAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        speedSpinner.setAdapter(speedAdapter);
        speedSpinner.setSelection((int)(RemoteStopClockServiceInterface.getInstance().getCannedRouteSpeed() / 2) - 1);
        
        CheckBox cannedBox = (CheckBox)findViewById(R.id.cannedChk);
        cannedBox.setOnCheckedChangeListener(cannedBoxListener);
        cannedBox.setChecked(RemoteStopClockServiceInterface.getInstance().getCannedRouteEnabled());
        
        enableControls(cannedBox.isChecked());
        
    }

	private OnItemSelectedListener routeSpinnerListener = new OnItemSelectedListener()
	{

		public void onItemSelected(AdapterView parent, View v, int position, long id) {
			if (!RemoteStopClockServiceInterface.getInstance().isClockRunning())
			{
				RemoteStopClockServiceInterface.getInstance().setCannedRouteId(position);
			}
		}

		public void onNothingSelected(AdapterView arg0) {
			if (!RemoteStopClockServiceInterface.getInstance().isClockRunning())
			{
				RemoteStopClockServiceInterface.getInstance().setCannedRouteId(1);
			}
		}
	
	};
	
	private OnItemSelectedListener speedSpinnerListener = new OnItemSelectedListener()
	{

		public void onItemSelected(AdapterView parent, View v, int position, long id) {
			RemoteStopClockServiceInterface.getInstance().setCannedRouteSpeed(2.0 * (position+1));
		}

		public void onNothingSelected(AdapterView arg0) {
			RemoteStopClockServiceInterface.getInstance().setCannedRouteSpeed(1.0);
		}
	
	};

	
	private OnCheckedChangeListener cannedBoxListener = new OnCheckedChangeListener()
	{

		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			enableControls(isChecked);
		}
		
	};

	private void enableControls(boolean enabled)
	{
        Spinner speedSpinner = (Spinner)findViewById(R.id.speedSpinner);
        Spinner routeSpinner = (Spinner)findViewById(R.id.routeSpinner);

        if (enabled)
		{
        	speedSpinner.setEnabled(true);
		}
		else
		{
        	speedSpinner.setEnabled(false);
		}

        if (enabled && !RemoteStopClockServiceInterface.getInstance().isClockRunning())
        {
        	routeSpinner.setEnabled(true);
        }
        else
        {
        	routeSpinner.setEnabled(false);
        }
        
        RemoteStopClockServiceInterface.getInstance().setCannedRouteEnabled(enabled);
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
        CheckBox cannedBox = (CheckBox)findViewById(R.id.cannedChk);

		if (RemoteStopClockServiceInterface.getInstance().isClockRunning())
		{
			cannedBox.setEnabled(false);
		}
		else
		{
			cannedBox.setEnabled(true);
		}
        enableControls(cannedBox.isChecked());

	}
	
	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockServiceInterface.getInstance().activityStopped(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockServiceInterface.getInstance().activityStarted(this);
	}

}
